package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {
    /**
     * 获取list中的map
     *
     * @param key
     * @param name 可变参数--数组
     * @return
     */
    @Override
    public Map<String, Object> getListMap(String key, String... name) {
        //listMap集合下有三组子数据key,name,children
        Map<String, Object> listMap = new HashMap<>();
        //先给key,name赋值
        listMap.put("name", name[0]);
        listMap.put("key", key);
        //children:List<Map<String,Object>>,定义集合存放children下数据
        List<Map<String, Object>> children = new ArrayList<>();
        //依次查询children下的子数据
        for (int i = 0; i < name.length - 1; i++) {
            Map<String, Object> childrenMap = new HashMap<>();
            //"基础配置"(父),(子)"站点配置", "底部信息", "其他配置"
            //children下的子数据的子数据,name,data数据
            String childrenName = name[i + 1];
            childrenMap.put("name", childrenName);
           //构造查询条件,查出子数据
            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            //sys_config表中的TabValue值与父级的key值匹配
            wrapper.eq(SysConfig::getTabValue, key);
            //GroupValue值匹配父级childrenName
            wrapper.eq(SysConfig::getGroupValue, childrenName);
            //查出数据
            List<SysConfig> childrenData = this.list(wrapper);
            //数据存入children下的data
            childrenMap.put("data", childrenData);

            children.add(childrenMap);
        }

        listMap.put("children", children);

        return listMap;

    }

    /**
     * 获取系统明细中的list
     * @return
     */
    @Override
    public List<Map<String, Object>> getList() {
        //设置list
        List<Map<String, Object>> list = new ArrayList<>();
        //list下面有3类配置信息(基础,系统,其他)，分别定义3个map集合,每个map封装name,key,children 数据
        //map1:基础配置,定义getListMap()方法获取具体数据
        Map<String, Object> listMap1 = getListMap("base", "基础配置", "站点配置", "底部信息", "其他配置");
        //获取到基础配置下的所有数据
        List<Map<String, Object>> children = (List<Map<String, Object>>) listMap1.get("children");

        //map2:系统设置
        Map<String, Object> listMap2 = getListMap("system", "系统设置", "管理系统配置");

        //map3:其他设置
        Map<String, Object> listMap3 = getListMap("custom", "其他设置", "通知公告", "前台图片");

        list.add(listMap1);
        list.add(listMap2);
        list.add(listMap3);

        return list;

    }


}

